package com.sl.u_push_plugin

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

/** UPushPlugin */
public class UPushPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    private lateinit var channel: MethodChannel
    private lateinit var eventChannel: EventChannel

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        MessageManager.instance.receiveMessageListener(flutterPluginBinding.applicationContext)
        channel = MethodChannel(flutterPluginBinding.flutterEngine.dartExecutor, "u_push_plugin")
        eventChannel = EventChannel(flutterPluginBinding.flutterEngine.dartExecutor, "u_push_plugin_e")
        eventChannel.setStreamHandler(object : EventChannel.StreamHandler {
            override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                MessageManager.instance.initEvents(events)
                PushManager.instance.initEvents(events)
            }

            override fun onCancel(arguments: Any?) {
            }
        })
        channel.setMethodCallHandler(this);
    }

    companion object {
        @JvmStatic
        fun registerWith(registrar: Registrar) {
            val channel = MethodChannel(registrar.messenger(), "u_push_plugin")
            channel.setMethodCallHandler(UPushPlugin())
            MessageManager.instance.receiveMessageListener(registrar.context())
            var eventChannel = EventChannel(registrar.messenger(), "u_push_plugin_e")
            eventChannel.setStreamHandler(object : EventChannel.StreamHandler {
                override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                    MessageManager.instance.initEvents(events)
                    PushManager.instance.initEvents(events)
                }

                override fun onCancel(arguments: Any?) {
                }
            })
        }
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        when (call.method) {
            "showNotificaitonOnForeground" -> {
                PushManager.instance.changeShowNotificaitonOnForeground(call.argument<Boolean>("show")!!)
            }
            "setNoDisturbMode" -> {
                PushManager.instance.setNoDisturbMode(call.argument<Int>("startHour")!!
                        , call.argument<Int>("endHour")!!
                        , call.argument<Int>("startMin")!!
                        , call.argument<Int>("endMin")!!)
            }
            "getCacheMessage" -> {
                MessageManager.instance.getCacheMessage(result)
            }
            "getDeviceToken" -> {
                result.success(PushManager.instance.getPushDeviceToken())
            }
            "openPush" -> {
                PushManager.instance.openPush(result)
            }
            "closePush" -> {
                PushManager.instance.closePush(result)
            }
            "addTags" -> {
                PushManager.instance.addTags(call.argument<List<String>>("tags")!!, result)
            }
            "deleteTags" -> {
                PushManager.instance.deleteTags(call.argument<List<String>>("tags")!!, result)
            }
            "getTags" -> {
                PushManager.instance.getTags(result)
            }
            "addAlias" -> {
                PushManager.instance.addAlias(call.argument<String>("aliasId")!!, call.argument<String>("aliasType")!!, result)
            }
            "setAlias" -> {
                PushManager.instance.setAlias(call.argument<String>("aliasId")!!, call.argument<String>("aliasType")!!, result)
            }
            "deleteAlias" -> {
                PushManager.instance.deleteAlias(call.argument<String>("aliasId")!!, call.argument<String>("aliasType")!!, result)
            }
            "UMSetLogEnabled" -> {
                PushManager.instance.setLogEnable(call.argument<Boolean>("logEnabled")!!)
            }
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    override fun onDetachedFromActivity() {
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        PushManager.instance.initContext(binding.activity.applicationContext)
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }
}
